from sklearn.metrics import average_precision_score
import pandas as pd


def calculate_map(df, ground_truth_column, prediction_column):
    y_true = df[ground_truth_column]
    y_scores = df[prediction_column]

    map_value = average_precision_score(y_true, y_scores)
    return map_value


# 示例用法
data = {
    'ground_truth': [0, 1, 1, 0, 1, 0, 1],
    'prediction': [0.3, 0.1, 0.5, 0.4, 0.2, 0.6, 0.7]
}

df = pd.DataFrame(data)
map_value = calculate_map(df, 'ground_truth', 'prediction')
print('mAP:', map_value)
